/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package functions;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Set;
import java.util.TreeMap;
import model.Diagnose;
import model.Drug;
import model.Item;
import model.Operation;
import stuff.FDate;

/**
 *
 * @author JArik
 */
public class HealingSet {

    private ArrayList<ItemList<Item>> visits;
    private String result="",name="";
    private Date firstVisit, lastVisit;
    private int id = -1;
    public HealingSet()
    {
        visits = new ArrayList<ItemList<Item>>();
        result="";
    }
    public HealingSet(int nId)
    {
        id = nId;
        visits = new ArrayList<ItemList<Item>>();
        result="";
    }
    public HealingSet(ArrayList<ItemList<Item>> nVis)
    {
        visits = nVis;
        visits = new ArrayList<ItemList<Item>>();
        result="";
    }
    public void setId(int nId)
    {
        id = nId;
    }
    public void setLastVisit(Date nDate)
    {
        lastVisit = nDate;
    }
    public void setName(String string) {
        name = string;
    }
    public void setFirstVisit(Date nDate)
    {
        firstVisit = nDate;
    }

    public int size()
    {
        return visits.size();
    }
    public ItemList<Item> getVisit(int i)
    {
        return visits.get(i);
    }
    public void addVisit(ItemList<Item> vis)
    {
        visits.add(vis);
    }
    public String getName()
    {
        return name;
    }
    public int getId()
    {
        return id;
    }
    public String getResult()
    {
        return result;
    }
    public void setResult(String res)
    {
        result = res;
    }
    public ArrayList<Diagnose> getDiagSummary()
    {
        TreeMap diags = new TreeMap();
        Diagnose diag;
        for(int i=0; i<visits.size();i++)
            for(int y=0;y<visits.get(i).size(); y++)
            {
                if(visits.get(i).get(y) instanceof Diagnose)
                {
                  diag =   (Diagnose)visits.get(i).get(y);
                  if(diag != null && diag.getName() != null)
                  if(!diags.containsKey(diag.getName()) || !diag.isPredv())
                     diags.put(diag.getName(), diag);
                }
            }
        
        ArrayList<Diagnose> arrDiag = new ArrayList<Diagnose>(diags.values());

        return arrDiag;
    }
    public ArrayList<Drug> getDrugSummary()
    {
        //TreeMap drugs = new TreeMap();
        ArrayList<Drug> arrDrug = new ArrayList<Drug>();
        Drug drug;
        for(int i=0; i<visits.size();i++)
            for(int y=0;y<visits.get(i).size(); y++)
            {
                if(visits.get(i).get(y) instanceof Drug)
                {
                  drug = new Drug();
                  drug.setDisDate(visits.get(i).get(y).getDisDate());
                  drug.setName(visits.get(i).get(y).getName());
                  drug.setAmount(visits.get(i).get(y).getAmount());
                  /*if(!drugs.containsKey(drug.getName()))
                     drugs.put(drug.getName(), drug);
                  else
                  {
                      drug.setAmount(
                             ((Drug)drugs.get(drug.getName())).getAmount()+drug.getAmount());
                     drugs.put(drug.getName(),drug);
                  }*/
                  arrDrug.add(drug);
                }
            }

        return arrDrug;
    }
    public ArrayList<Operation> getOperationSummary()
    {
        ArrayList<Operation> opers = new ArrayList<Operation>();
        Operation oper;
        for(int i=0; i<visits.size();i++)
            for(int y=0;y<visits.get(i).size(); y++)
            {
                if(visits.get(i).get(y) instanceof Operation)
                {
                  oper =   (Operation)visits.get(i).get(y);
                  opers.add(oper);
                }
            }
        return opers;
    }
    public Date getLastVisit() {
        if(lastVisit == null) return null;
        return new FDate(lastVisit.getTime());
    }
    public Date getLastSQLVisit()
    {
        if(lastVisit == null) return null;
        return new Date(lastVisit.getTime());
    }
    public Date getFirstVisit()
    {
        if(firstVisit == null) return null;
        return new FDate(firstVisit.getTime());
    }

    public Date getFirstSQLVisit()
    {
        if(firstVisit == null) return null;
        return new Date(firstVisit.getTime());
    }

    public String toString()
    {
        return name;
    }
}